//
//  LYNavigationController.swift
//  Swift---新浪微博练习代码
//
//  Created by Mr.Yan on 2017/6/30.
//  Copyright © 2017年 Mr.Yan. All rights reserved.
//

import UIKit

class LYNavigationController: UINavigationController,UIGestureRecognizerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //重写手势代理
        interactivePopGestureRecognizer?.delegate = self
    }
    //重写父类方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        // 判断count 大于0 (一级界面去掉)
        if childViewControllers.count > 0 {
            // 设置默认值
            var title = "返回"
            // 二级界面 显示是上一个界面的title 其它的是显示返回
            if childViewControllers.count == 1 {
                title = childViewControllers.first?.navigationItem.title ?? ""
                //隐藏tabBar
                viewController.hidesBottomBarWhenPushed = true
            }
            
            // 设置左侧按钮
            // 如果自定义了左侧导航按钮 会导致点击屏幕左侧 右滑手势失效
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_back_withtext", title: title, tager: self, action: #selector(backClick))
        }
        
        // 监听该方法完成左侧按钮设置
        super.pushViewController(viewController, animated: animated)
    }
    func backClick(){
        popViewController(animated: true)
    }
    //监听边缘手势
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return  (childViewControllers.count != 1)
    }
}
